package models;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import play.db.jpa.Model;
import util.PageUtil;

@Entity
@Table(name = "sale")
public class Sale extends Model {

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "member_id")
	public Member member;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "product_id")
	public Product product;
	@Column(name = "available")
	public int available;
	
	public static enum AvaiableStatus {
		in(1), end(0), cancel(-1);
		
		private int stat_value;
		AvaiableStatus(int stat) {
			this.stat_value = stat;
		}

		public int getStatValue() {
			return stat_value;
		}
	}
	
	
	@Column(name = "contract")
	public String contract;
	@Column(name = "depict",length=65535)
	public String depict;
	@Column(name = "email")
	public String email;
	@Column(name = "pic")
	public String pic;
	@Column(name = "price")
	public String price;
	@Column(name = "status")
	public String status;
	@Column(name = "tel")
	public String tel;
	@Column(name = "time", length = 19)
	public Date time;
	@Column(name = "title")
	public String title;
	@Column(name = "area", length = 40)
	public String area;
	
	public Sale() {
	}

	public Sale(Member member, Product product, int available, String contract, String depict, String email,
			String pic, String price, String status, String tel, Date time, String title,String area) {
		this.member = member;
		this.product = product;
		this.available = available;
		this.contract = contract;
		this.depict = depict;
		this.email = email;
		this.pic = pic;
		this.price = price;
		this.status = status;
		this.tel = tel;
		this.time = time;
		this.title = title;
		this.area=area;
	}
	
	public static Sale getDetail(long saleID){
		
		String hql="from Sale sale inner join fetch sale.product " +
					"inner join fetch sale.product.types " +
					"inner join fetch sale.product.company " +
					"inner join fetch sale.member " +
					"where sale.id=?";
		
		return find(hql,saleID).first();
		
	}
	
	
	
	
	/**
	 * 分页查找特定类型的出售信息
	 * @param page
	 * @param size
	 * @param typeID
	 * @return
	 */
	public static List<Sale> getSales(int pageNum,int pageSize,long typeID){
		
		
		String hql="from Sale sale " +
				   "inner join fetch sale.product " +
				   "inner join fetch sale.product.types " +
				   "inner join fetch sale.product.company where sale.product.types.id=?";
		
		return find(hql,typeID).from(PageUtil.getPageBegin(pageNum,pageSize)).fetch(pageSize);
	}
	

	public static long getTypesCount(long typeID){
		
		String hql="from Sale sale " +
				    "inner join  sale.product as product " +
					"inner join  product.types " +
					"inner join  product.company where product.types.id=?";

		long count = Sale.count(hql, typeID);
		return count;
	}
	
	public static List<Sale>  findByMemberID(long memberid,int limit){
		
		String hql="from Sale sale  where sale.member.id=?";
		List<Sale> result=null;
		if(limit>0){
			result=Sale.find(hql, memberid).fetch(limit);
		}else{
			result=Sale.find(hql, memberid).fetch();
		}
		return result;
	}

}